home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MacStarter (THINK C 5.0⁄6.0) / other applicationProcs files / scrollDemo.c < prev    next >
Text File  |  1994-01-25  |  9KB  |  254 lines

  1. /* This file demonstrates simple use of scroll bars in MacStarter.
  2.    Just for fun, it also illustrates how to change the cursor style.
  3.    To use this file, you should add it to the MacStarter.π project
  4.    and delete the current applicationProcs file.
  5.        All places where this file has been modified (from the 
  6.    original applicationProcs.c) are commented with comments
  7.    that begin with //
  8. */
  9.  
  10. #include "globals-MacStarter.h"
  11.  
  12. long gEventWaitTime = 3;   // a value of 3 indicates that this program 
  13.                            // wants function ApplicationIdle to be called
  14.                            // every 3/60-th seconds (if possible).  This 
  15.                            // seems to be a reasonable interval when
  16.                            // ApplicationIdle is setting the cursor style.
  17.                            
  18. CursHandle fingerCursor; // global variable for a cursor that looks
  19.                          // like a pointing hand.  This cursor is loaded
  20.                          // in InitApplication                          
  21.                          
  22.  
  23. MenuHandle editMenu, fileMenu;
  24.  
  25. void InitApplication(void);
  26. void UpdateMenus(void);
  27. void DoEditMenu(int itemNum);
  28. void DoFileMenu(int itemNum, int* done);
  29. void DoOtherMenu(int menuID, int itemNum);
  30. void ApplicationIdle(void);
  31. void CleanUpApplication(void);
  32. void AboutBox(void);
  33. void DoNewCommand(void);
  34.  
  35.  
  36. class myWindow : public xWindow {
  37.   public:
  38.    virtual void OpenInRect(Str255 title, int left, int top, int right, int bottom);   
  39.    virtual short Close(void);
  40.   protected:
  41.    virtual void SetDefaults(void);
  42.    virtual void doKey(char ch);
  43.    virtual void doContentClick(Point localPt);
  44.    virtual void adjustToNewSize(void);
  45.    virtual void doRedraw(Rect* badRect);
  46.    virtual void doHScroll(int dh);
  47.    virtual void doVScroll(int dv);
  48.    virtual void doActivate(int active);
  49. };
  50.  
  51. void myWindow::SetDefaults(void) {
  52.    inherited::SetDefaults();
  53.    hLinesPerPage = 15;   // A click in the gray area of the horizontal scroll
  54.                          // bar will have an effect 15 times as big as a 
  55.                          // click on on arrow.
  56.    vLinesPerPage = 15;   // Similarly for the vertical scroll bar.
  57. }
  58.  
  59. void myWindow::OpenInRect(Str255 title, int left, int top, int right, int bottom) {
  60.    inherited::OpenInRect(title,left,top,right,bottom);
  61. }
  62.  
  63. short myWindow::Close(void) {
  64.    inherited::Close();
  65. }
  66.  
  67. void myWindow::doKey(char ch) {
  68. }
  69.  
  70. void myWindow::doContentClick(Point localPt) {
  71. }
  72.  
  73.  
  74. // Function doRedraw draws a rectangle whose height and width depend
  75. // on the current scroll bar settings.  Since this procedure is
  76. // called by doHScroll or doVScroll when the user clicks on the scroll bar,
  77. // the size of the rectangle changes while the user continues to scroll.
  78. // Note the flickering effect caused by the use of the default doHScroll
  79. // and doVScroll, which erase the screen before calling doRedraw.
  80.  
  81. void myWindow::doRedraw(Rect* badRect){
  82.    Rect R;
  83.    short hVal,vVal;
  84.    R = theWindow->portRect;  // this is a rectangle that covers the whole window
  85.    R.bottom = R.bottom - 15; // don't include space occupied by horizontal scroll
  86.    R.right = R.right - 15;   // or vertical scroll
  87.    hVal = GetHVal();  // get current setting of horizontal scroll bar
  88.    vVal = GetVVal();  // get current setting of vertical scroll bar
  89.    InsetRect(&R,hVal,vVal);  // remove a border from around the rectangle
  90.                              // with size depending on hVal and vVal
  91.    PaintRect(&R);   // Fill in the remaining rectangle
  92. }
  93.  
  94.  
  95. // Sets the maximum values on the horizontal and vertical scrolls to
  96. // appropriate values for the window.
  97.  
  98. void myWindow::adjustToNewSize(void) {
  99.    Rect R;
  100.    short height,width;
  101.  
  102.    inherited::adjustToNewSize();
  103.  
  104.    R = theWindow->portRect;  // this is a rectangle that covers the whole window
  105.    R.bottom = R.bottom - 15; // don't include space occupied by horizontal scroll
  106.    R.right = R.right - 15;   // or vertical scroll
  107.  
  108.    height = R.bottom - R.top;  // height of window
  109.    width = R.right - R.left;    // width of window
  110.  
  111.    SetHMax((width-1) / 2);   // set maximum possible value of scroll bars
  112.    SetVMax((height-1) / 2);
  113.    // NOTE: The values used here are simply the largest values by which
  114.    //       the rectangel can be "inset" in function doRedraw without
  115.    //       actually making the rectangle disappear.
  116. }
  117.  
  118.  
  119. void myWindow::doHScroll(int dh) {
  120.    inherited::doHScroll(dh);
  121. }
  122.  
  123. void myWindow::doVScroll(int dv) {
  124.    inherited::doVScroll(dv);
  125. }
  126.  
  127. void myWindow::doActivate(int active) {
  128.    inherited::doActivate(active);
  129. }
  130.  
  131. void InitApplication(void) {
  132.   MenuHandle appleMenu;
  133.   fingerCursor = GetCursor(129);  // load the "pointing finger cursor"
  134.                                   // from the resource file.
  135.   fileMenu = GetMHandle(2);
  136.   editMenu = GetMHandle(3);
  137.   appleMenu = GetMHandle(1);
  138.   SetItem(appleMenu,1,"\pAbout StupidScroll...");  // Sets the name of the
  139.   DoNewCommand();                                  // program in first line of
  140. }                                                  // the Apple menu
  141.  
  142. void UpdateMenus(void) {
  143.    short i;
  144.    WindowPtr win;
  145.    xWindow *xwin;
  146.    win = FrontWindow();
  147.    if ( win && ((WindowPeek)win)->windowKind < 0 ) {  
  148.       EnableItem(editMenu,1);
  149.       for (i=3; i<7; i++)
  150.          EnableItem(editMenu,i);
  151.    }
  152.    else {
  153.       DisableItem(editMenu,1);
  154.       for (i=3; i<7; i++)
  155.          DisableItem(editMenu,i);
  156.    }
  157.    if (win && xWindow::Window2XWindow(win,&xwin)) {
  158.       EnableItem(fileMenu,2);
  159.    }
  160.    else {
  161.       DisableItem(fileMenu,2);
  162.    }
  163. }
  164.  
  165. void DoEditMenu(int itemNum) {
  166. }
  167.  
  168. void DoFileMenu(int itemNum, int* done) {
  169.    xWindow *win;
  170.    if (itemNum == 4)
  171.       *done = 1;
  172.    else if (itemNum == 1)
  173.       DoNewCommand();
  174.    else if (itemNum == 2 && xWindow::Window2XWindow(FrontWindow(),&win))
  175.       win->Close();
  176. }
  177.  
  178. void DoOtherMenu(int menuID, int itemNum) {
  179. }
  180.  
  181.  
  182. // ApplicationIdle checks the current mouse position and uses it to determine
  183. // which style of cursor should be used.  In this program, the arrow
  184. // cursor is used except when the mouse is over a scroll bar; in that
  185. // case, a pointing finger cursor is used.
  186. //    Hint:  If you get serious about cursor-setting, you will probably
  187. // want to put a SetCursor(&arrow) at the beginning of each menu-handling
  188. // procedure. Otherwise, you can get stuck with a funny cursor while
  189. // a menu command is being carried out.  (This is annoying if the
  190. // command brings up a dialog box.)  Note that "arrow" is a predefined
  191. // variable of type Cursor.
  192.  
  193. void ApplicationIdle(void) {
  194.  
  195.    Point pt;        // mouse location
  196.    WindowPtr win;   // window contining mouse (if any)
  197.    xWindow *xwin;   // the xWindow corresponding to win
  198.    short partNum;   // Returned by FindWindow to tell what the
  199.                     //   mouse is currently over.
  200.    GrafPtr savePort;   // I need this because I have to set the drawing
  201.                        //   port in order to use the function GlobalToLocal
  202.    ControlHandle theControl;   // If the mouse is over a window, I have to
  203.                                // check whether it is over a "control"
  204.                                // which in this program can only be a
  205.                                // scroll bar in the frontmost window.
  206.  
  207.    pt = gEvent.where; // mouse loc is taken from global event record gEvent
  208.                       // (This might be slightly out-of-date; you could
  209.                       // get a new event record using EventAvail() if you want.)
  210.    partNum = FindWindow(pt, &win);  // Sets win=0 if the mouse is not 
  211.                       // over a window; if it is, partNum will have the
  212.                       // value inContent when the mouse is over the inside
  213.                       // of the window.
  214.    if ( win && partNum == inContent && xWindow::Window2XWindow(win,&xwin) ) {
  215.                 // The last check is here because I want to ignore any
  216.                 // window that wasn't created by this program.
  217.       GetPort(&savePort);
  218.       SetPort(win);
  219.       GlobalToLocal(&pt);  // FindControl requires a point in window coords
  220.       SetPort(savePort);
  221.       FindControl(pt,win,&theControl);
  222.                 // if the mouse is over a control, FindControl sets
  223.                 // theControl to be a handle to that control, hence
  224.                 // non-zero.  I also check that fingerCursor is non-zero;
  225.                 // It would be zero if for some reason the cursor couldn't
  226.                 // loaded from the resource file.
  227.       SetCursor( (theControl && fingerCursor) ? *fingerCursor : &arrow );
  228.    }
  229.    else
  230.       SetCursor(&arrow);  // Make sure to set the cursor to something or
  231.                           // other in this function.  (It is easier to
  232.                           // think in terms of continually "setting"
  233.                           // the cursor, rather than trying to "change"
  234.                           // it only when necessary.)
  235. }
  236.  
  237. void CleanUpApplication(void) {
  238. }
  239.  
  240.  
  241. // Set up the About Box to describe this program
  242. void AboutBox(void) {  
  243.    ParamText( "\pScroll Demo",
  244.               "\pDavid Eck",
  245.               "\pHobart and William Smith College\rGeneva, NY  14456\rE-mail: eck@hws.bitnet",
  246.               "\pDemonstrates the use of scroll bars in an application written using the Macintosh application shell MacStarter.");
  247.    Alert(128,0L);
  248. }
  249.  
  250. void DoNewCommand(void) {
  251.    myWindow *win;
  252.    win = new myWindow;
  253.    win->Open("\pSample Window");
  254. }